from rest_framework import serializers
from .models import Comment, CommentReply, CommentReport


class CommentReplySerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    
    class Meta:
        model = CommentReply
        fields = ['id', 'user', 'content', 'created_at']
        read_only_fields = ['id', 'user', 'created_at']


class CommentListSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    replies = CommentReplySerializer(many=True, read_only=True)
    book_title = serializers.CharField(source='book.title', read_only=True)
    
    class Meta:
        model = Comment
        fields = [
            'id', 'book', 'book_title', 'user', 'title', 'content', 
            'rating', 'is_verified', 'helpful_count', 'unhelpful_count',
            'replies', 'created_at', 'updated_at'
        ]


class CommentDetailSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    replies = CommentReplySerializer(many=True, read_only=True)
    
    class Meta:
        model = Comment
        fields = [
            'id', 'book', 'user', 'title', 'content', 'rating',
            'is_approved', 'is_verified', 'helpful_count', 'unhelpful_count',
            'replies', 'created_at', 'updated_at'
        ]


class CommentCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = ['book', 'title', 'content', 'rating']
    
    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class CommentAdminSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    book = serializers.StringRelatedField(read_only=True)
    
    class Meta:
        model = Comment
        fields = [
            'id', 'book', 'user', 'title', 'content', 'rating',
            'is_approved', 'is_verified', 'approved_by', 'approved_at',
            'helpful_count', 'unhelpful_count', 'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'book', 'user', 'created_at', 'updated_at']


class CommentReportSerializer(serializers.ModelSerializer):
    reporter = serializers.StringRelatedField(read_only=True)
    comment_info = serializers.SerializerMethodField()
    
    class Meta:
        model = CommentReport
        fields = [
            'id', 'comment', 'comment_info', 'reporter', 'reason', 
            'description', 'status', 'admin_note', 'created_at'
        ]
        read_only_fields = ['id', 'reporter', 'created_at']
    
    def get_comment_info(self, obj):
        return {
            'book': obj.comment.book.title,
            'user': str(obj.comment.user),
            'content': obj.comment.content[:100] + '...' if len(obj.comment.content) > 100 else obj.comment.content
        }
    
    def create(self, validated_data):
        validated_data['reporter'] = self.context['request'].user
        return super().create(validated_data)


class CommentReportAdminSerializer(serializers.ModelSerializer):
    reporter = serializers.StringRelatedField(read_only=True)
    comment = serializers.StringRelatedField(read_only=True)
    
    class Meta:
        model = CommentReport
        fields = [
            'id', 'comment', 'reporter', 'reason', 'description',
            'status', 'admin_note', 'processed_by', 'processed_at', 'created_at'
        ]
        read_only_fields = ['id', 'comment', 'reporter', 'reason', 'description', 'created_at']


class CommentVoteSerializer(serializers.Serializer):
    vote_type = serializers.ChoiceField(choices=['helpful', 'unhelpful'])
